import comfirmLogin from '@/components/comfirm.vue';
import {mobileTest,reLoginWX } from '@/util/util.js';
import { Toast } from 'mint-ui';

import { mapState,mapMutations } from 'Vuex';
export default {
  name: "login",
  data: () => ({
    phone:'',
    code:'',
    getcode:'',     //请求获取的验证码
    comfirmTitle:{
      comfirmTop:'提示',
      comfirmCon:'验证码错误',
      comfirmBtn:['取消','确认'],
    },
    isWrong:false,   //显示确认框
    isGetYzmTime:false, //倒计时内不能重复点击
    yzmTime:60,
  }),
  components: {
    comfirmLogin,
  },
  watch:{
    phone(newValue,oldValue){ // 监听
      // console.group('< === 是否是输入 === >')
      // console.log("%c%s", "color:red", `${newValue.length > oldValue.length}`)
      // console.groupEnd('< === 是否是输入 === >')
      // if (newValue.length > oldValue.length) {
      //   console.log('1');
      //     this.phone = newValue.replace(/\s/g, '').replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3')
      //   } else {
      //     console.log('2');
      //     this.phone = this.phone.trim()
      //   }
      //简写优化
      this.phone = newValue.length > oldValue.length ? newValue.replace(/\s/g, '').replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3') : this.phone.trim();
    }
  },
  computed:{
    ...mapState({
      token:state=>state.login.token,
      userInfo:state=>state.login.userInfo,
      relogin:state=>state.login.relogin,
    }),

  },
  mounted() {
    //do something after mounting vue instance
    let userToken = window.localStorage.getItem('userToken');   //获取缓存中的userToken
    let syOpenId = window.localStorage.getItem('syOpenId');     //获取缓存中的openi
    if (syOpenId==undefined || syOpenId=='null') {    //未授权
      reLoginWX();    //重新微信授权
    }
  },
  methods: {
    ...mapMutations([
      'setRelogin',    //是否重新登录
      'setUserInfo',
    ]),
    trim(x){
      return x.replace(/\s/g,"");
    },
    getCode () {
      var that = this;
      var phone = this.trim(that.phone);
      console.log(phone);
      if(phone==''){
        Toast('请输入手机号码');
        return false;
      }
      if(mobileTest(phone)){
        Toast('请填写正确的手机号码');
        return false;
      }
      var yzmParam = {
        'mobile':phone,
        'pinType':1       //验证码类型
      }
      network.get(configUrl.LOGINGETYZM_URL,yzmParam,function(res){
        console.log(res);
        that.getcode = res;
        Toast('验证码发送成功');
        that.yzmTime = 60;
        that.isGetYzmTime = true;

        let yzmInterval = setInterval(()=>{
          that.yzmTime --;
          if(that.yzmTime==0){
            that.isGetYzmTime = false;
            return false;
          }
        },1000);

      },function(err){
        console.log(err);
      });
    },
    // 验证码输入验证
    yzmCodeIpt() {
      console.log(this.code);
      let code = this.code;
      if(code.length==6){
        if(code!='666666'){
          this.isWrong = true;
        }else{
          this.isWrong = false;
        }
      }else if(code.length>6){
        return false;
      }
    },
    //登录
    login () {
      console.log(configUrl.LOGIN_URL);
      var that = this;
      var phone = that.trim(that.phone);    //去掉空格后的手机号码
      var yzmCode = that.code;
      let openId = window.localStorage.getItem('syOpenId');
      let icon = window.localStorage.getItem('syIcon');
      if(phone==''){
        Toast('请输入手机号码');
        return false;
      }
      if(yzmCode==''){
        Toast('请填写验证码');
        return false;
      }

      var loginParam = {
        'mobile':phone,
        'yzmCode':yzmCode,
        'openId':openId,
        'icon':icon
      }
      network.post(configUrl.LOGIN_URL,loginParam,function(res){
        console.log(res);
        var data = res.data;

        window.localStorage.setItem('userInfo',JSON.stringify(data));   //本地缓存用户信息
        window.localStorage.setItem('userToken',data.token);   //本地缓存用户信息
        that.setUserInfo(res.data);
        Toast('登录成功');
        setTimeout(function() {
          // that.$router.push({name:'index',param:{}});
          that.setRelogin();
          that.$router.go(-1);
        },2000);

      },function(err){
        console.log(err);
      });

      // this.$router.push({name:'index',param:{}})    //跳转到首页
    },

    comfirmEvent (data) {
      console.log(data);
      var that = this;
      that.isWrong = data.isWrong;
      if(data.isTrue){
        //点击了确定
      }else{
        //点击了取消
      }
    }
  },
}
